package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.innovation.chengzhangriji.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public interface OnClickNoteListener {
        void onClickNote(NoteViewHolder viewHolder);

        void onLongClickNote(NoteViewHolder viewHolder);
    }

    private OnClickNoteListener mOnClickNoteListener;

    private CuteTwoImageView mImageNoteBg;

    private RoundedImageView mImageNoteHead;

    private CuteTextView mTextName;

    private DisplayImageOptions options; //配置图片加载及显示选项

    public NoteViewHolder(View itemView) {
        super(itemView);
        mImageNoteBg = (CuteTwoImageView) itemView.findViewById(R.id.text_note_bg);
        mImageNoteHead = (RoundedImageView) itemView.findViewById(R.id.text_note_head);
        mTextName = (CuteTextView) itemView.findViewById(R.id.text_name);
        mImageNoteBg.setOnClickListener(this);
        mImageNoteBg.setOnLongClickListener(this);
        setOptions();
    }

    @Override
    public void onClick(View v) {
        if (mOnClickNoteListener == null) {
            return;
        }
        if (v.getId() == R.id.text_note_bg) {
            mOnClickNoteListener.onClickNote(this);
        }
    }


    @Override
    public boolean onLongClick(View v) {
        if (mOnClickNoteListener != null && v.getId() == R.id.text_note_bg) {
            mOnClickNoteListener.onLongClickNote(this);
        }
        return true;
    }

    public void setOnClickNoteListener(OnClickNoteListener onClickNoteListener) {
        mOnClickNoteListener = onClickNoteListener;
    }

    public void open() {
        mImageNoteBg.setChangeImage();
    }

    public void close() {
        mImageNoteBg.setInitImage();
    }

    public void setNoteHead(String imageUrl) {
        ImageLoader.getInstance().displayImage(imageUrl, mImageNoteHead, options, null);
    }

    public void setNoteName(String name) {
        mTextName.setText(name);
    }

    // 配置图片加载及显示选项
    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_head_unknown)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_head_unknown)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_head_unknown)  //image加载失败
                        // .cacheInMemory(true)  //加载图片时会在内存中加载缓存
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
